<?php
global $icl_utility, $wpsc_cart, $wpdb, $wpsc_checkout, $wpsc_gateway, $wpsc_coupons;

$wpsc_checkout = new wpsc_checkout();
$wpsc_gateway = new wpsc_gateways();
$wpsc_coupons = new wpsc_coupons($_SESSION['coupon_numbers']);

$show_shopping_cart = (is_user_logged_in()) ? TRUE : FALSE;

if ($show_shopping_cart) :
if (wpsc_cart_item_count() > 0) :
?>
<div class="checkout">
<h2>Checkout</h2>
<form>
	<p>Confirm your totals before making the payment:</p>
	<hr class="hrbar"/>
</form>
<table class="payment" cellspacing="0" cellpadding="0" border="1">
	<tr>
		<th width="190">Product</th>
		<th width="140">Quantity</th>
		<th width="90">Price</th>
		<th width="80"> </th>
	</tr>
	<?php while (wpsc_have_cart_items()) : wpsc_the_cart_item(); ?>
	<tr>
		<td><?php echo wpsc_cart_item_name(); ?></td>
		<td>
			<form action="<?php echo get_option('shopping_cart_url'); ?>" method="post">
				<input type="text" name="quantity" size="2" value="<?php echo wpsc_cart_item_quantity(); ?>"/>
				<input type="hidden" name="key" value="<?php echo wpsc_the_cart_item_key(); ?>"/>
				<input type="hidden" name="wpsc_update_quantity" value="true"/>
				<input type="submit" value="<?php echo TXT_WPSC_APPLY; ?>" name="submit"/>
			</form>
		</td>
		<td><?php echo wpsc_cart_item_price(); ?></td>
		<td>
			<form id="" action="<?php echo get_option('shopping_cart_url'); ?>" method="post">
				<input type="hidden" name="quantity" value="0"/>
				<input type="hidden" name="key" value="<?php echo wpsc_the_cart_item_key(); ?>"/>
				<input type="hidden" name="wpsc_update_quantity" value="true"/>
				<button class='remove_button' type="submit"><span><?php echo TXT_WPSC_REMOVE; ?></span></button>
			</form>
		</td>
	</tr>
	<?php endwhile; ?>
	<?php //this HTML displays coupons if there are any active coupons to use ?>
	<?php if(wpsc_uses_coupons()): ?>
		<?php if(wpsc_coupons_error()): ?>
	<tr><td colspan="4"><?php echo TXT_WPSC_COUPONSINVALID; ?></td></tr>
		<?php endif; ?>
	<tr>
		<td colspan="2"><?php _e('Enter your coupon number'); ?> :</td>
		<td colspan="2" align='left'>
			<form  method='post' action="<?php echo get_option('shopping_cart_url'); ?>">
				<input type='text' name='coupon_num' id='coupon_num' value='<?php echo $wpsc_cart->coupons_name; ?>' />
				<input type='submit' value='<?php echo TXT_WPSC_APPLY ?>' />
			</form>
		</td>
	</tr>
	<?php endif; ?>
	<?php if(isset($_SESSION['nocamsg']) && isset($_GET['noca']) && $_GET['noca'] == 'confirm'): ?>
	<tr>
		<td colspan="4"><p class='validation-error'><?php echo $_SESSION['nocamsg']; ?></p></td>
	</tr>
	<?php endif; ?>
	<?php do_action('wpsc_before_shipping_of_shopping_cart'); ?>
	<?php if(wpsc_uses_shipping()) : ?>
	<tr>
		<td colspan="4"><h2><?php echo TXT_WPSC_SHIPPING_COUNTRY; ?></h2></td>
	</tr>
	<tr>
		<th colspan="4"><?php echo TXT_WPSC_SHIPPING_DETAIL; ?></th>
	</tr>
	<tr>
		<td colspan="4">
			<?php if($_SESSION['categoryAndShippingCountryConflict'] != '') : ?>
			<p class='validation-error'><?php echo $_SESSION['categoryAndShippingCountryConflict']; ?></p>
			<?php endif; ?>
			<?php if($_SESSION['WpscGatewayErrorMessage'] != '') : ?>
			<p class='validation-error'><?php echo $_SESSION['WpscGatewayErrorMessage']; ?></p>
			<?php endif; ?>
			<form name='change_country' id='change_country' action='' method='post'>
				<?php echo wpsc_shipping_country_list();?>
				<input type='hidden' name='wpsc_update_location' value='true' />
				<input type='submit' name='wpsc_submit_zipcode' value='Calculate' />
			</form>
		</td>
	</tr>
		
	<?php while (wpsc_have_shipping_methods()) : wpsc_the_shipping_method(); ?>
	<tr>
		<th colspan="4"><?php echo wpsc_shipping_method_name().TXT_WPSC_CHOOSE_A_SHIPPING_RATE; ?></th>
	</tr>
	<?php while (wpsc_have_shipping_quotes()) : wpsc_the_shipping_quote(); ?>
		<tr>
			<td colspan="2">
				<label for='<?php echo wpsc_shipping_quote_html_id(); ?>'><?php echo wpsc_shipping_quote_name(); ?></label>
			</td>
			<td style='text-align:center;'>
				<label for='<?php echo wpsc_shipping_quote_html_id(); ?>'><?php echo wpsc_shipping_quote_value(); ?></label>
			</td>
			<td style='text-align:center;'>
			<?php if(wpsc_have_morethanone_shipping_methods_and_quotes()): ?>
				<input type='radio' id='<?php echo wpsc_shipping_quote_html_id(); ?>' <?php echo wpsc_shipping_quote_selected_state(); ?>  onclick='switchmethod("<?php echo wpsc_shipping_quote_name(); ?>", "<?php echo wpsc_shipping_method_internal_name(); ?>")' value='<?php echo wpsc_shipping_quote_value(true); ?>' name='shipping_method' />
			<?php else: ?>
				<input <?php echo wpsc_shipping_quote_selected_state(); ?> disabled='disabled' type='radio' id='<?php echo wpsc_shipping_quote_html_id(); ?>'  value='<?php echo wpsc_shipping_quote_value(true); ?>' name='shipping_method' />
					<?php wpsc_update_shipping_single_method(); ?>
			<?php endif; ?>
			</td>
		</tr>
	<?php endwhile; ?>
	<?php endwhile;  ?>
	<?php wpsc_update_shipping_multiple_methods(); ?>
	<?php endif;  ?>
	<tr class="total">
		<td colspan="2"><?php echo TXT_WPSC_TAX; ?></td>
		<td colspan="2"><?php echo wpsc_cart_tax(); ?></td>
	</tr>
	<?php if(wpsc_uses_coupons() && (wpsc_coupon_amount() != 0)): ?>
	<tr class="total">
		<td colspan="2"><?php echo TXT_WPSC_COUPONS; ?></td>
		<td colspan="2"><?php echo wpsc_coupon_amount(); ?></td>
	</tr>
	<?php endif ?>
	<tr class="total">
		<td colspan="2"><?php echo TXT_WPSC_TOTALPRICE; ?></td>
		<td colspan="2"><?php echo wpsc_cart_total(); ?></td>
	</tr>
</table>

<?php do_action('wpsc_before_form_of_shopping_cart'); ?>

<form class='wpsc_checkout_forms' action='' method='post' enctype="multipart/form-data">
	<?php if(!is_user_logged_in() && get_option('users_can_register') && get_option('require_register')) : ?>
		<h2><?php _e('Not yet a member?');?></h2>
		<p><?php _e('In order to buy from us, you\'ll need an account. Joining is free and easy. All you need is a username, password and valid email address.');?></p>
		<?php	if(count($_SESSION['wpsc_checkout_user_error_messages']) > 0) : ?>
		<div class="login_error"> 
			<?php		  
			foreach($_SESSION['wpsc_checkout_user_error_messages'] as $user_error ) {
			  echo $user_error."<br />\n";
			}
			$_SESSION['wpsc_checkout_user_error_messages'] = array();
			?>			
		</div>
		<?php endif; ?>
		<fieldset class='wpsc_registration_form'>
			<label><?php _e('Username'); ?>:</label><input type="text" name="log" id="log" value="" size="20"/>
			<label><?php _e('Password'); ?>:</label><input type="password" name="pwd" id="pwd" value="" size="20" />
			<label><?php _e('E-mail'); ?>:</label><input type="text" name="user_email" id="user_email" value="<?php echo attribute_escape(stripslashes($user_email)); ?>" size="20" />
		</fieldset>
	<?php endif; ?>

	<h2><?php echo TXT_WPSC_CONTACTDETAILS; ?></h2>
	 Please enter your contact details:<br/>
	<strong><?php echo TXT_WPSC_CREDITCARDHANDY; ?></strong>
	<br/><br/>
	<?php echo TXT_WPSC_ASTERISK; ?>
	<hr class="hrbar"/>
	<?php
	if(count($_SESSION['wpsc_checkout_misc_error_messages']) > 0) {
		echo "<div class='login_error'>\n\r";
		foreach((array)$_SESSION['wpsc_checkout_misc_error_messages'] as $user_error ) {
			echo $user_error."<br />\n";
		}
		echo "</div>\n\r";
	}
	$_SESSION['wpsc_checkout_misc_error_messages'] =array();
	?>
	<div class="form">
	<?php while (wpsc_have_checkout_items()) : wpsc_the_checkout_item(); ?>
		<?php if(wpsc_checkout_form_is_header() == true) : ?>
		<h4><?php echo wpsc_checkout_form_name();?></h4>
		<?php endif; ?>
		<?php if(wpsc_is_shipping_details()) : ?>
		<label for="shippingSameBilling">Shipping Address same as Billing Address?</label>
		<div class="in">
			<input type='checkbox' value='true' name='shippingSameBilling' id='shippingSameBilling' />
		</div>
		<div class="clear"></div>
		<?php endif; ?>
		<?php if(wpsc_checkout_form_is_header() == true) : ?>
		<?php else : ?>
		<?php if((!wpsc_uses_shipping()) && $wpsc_checkout->checkout_item->unique_name == 'shippingstate'): ?>
		<?php else : ?>
		<label for="<?php echo wpsc_checkout_form_element_id(); ?>"><?php echo wpsc_checkout_form_name();?></label>
		<div class="in">
			<?php echo wpsc_checkout_form_field();?>
		    <?php if(wpsc_the_checkout_item_error() != ''): ?>
		    <p class='validation-error'><?php echo wpsc_the_checkout_item_error(); ?></p>
			<?php endif; ?>
		</div>
		<div class="clear"></div>
		<?php endif; ?>
		<?php endif; ?>
	<?php endwhile; ?>
	<?php if (get_option('display_find_us') == '1') : ?>
		<label for="find-us">How did you find us:</label>
		<div class="in">
			<select name='how_find_us'>
				<option value='Word of Mouth'>Word of mouth</option>
				<option value='Advertisement'>Advertising</option>
				<option value='Internet'>Internet</option>
				<option value='Customer'>Existing Customer</option>
			</select>
		</div>
		<div class="clear"></div>
	<?php endif; ?>
	<?php if(wpsc_gateway_count() > 1): // if we have more than one gateway enabled, offer the user a choice ?>
		<h4><?php echo TXT_WPSC_SELECTGATEWAY;?></h4>
		<?php while (wpsc_have_gateways()) : wpsc_the_gateway(); ?>
		<div class="in">
			<?php if(wpsc_gateway_internal_name() == 'noca') : ?>
			<input type="radio" id='noca_gateway' value="<?php echo wpsc_gateway_internal_name();?>" <?php echo wpsc_gateway_is_checked(); ?> name="custom_gateway" class="custom_gateway"/><?php echo wpsc_gateway_name();?>
			<?php else : ?>
			<input type="radio" value="<?php echo wpsc_gateway_internal_name();?>" <?php echo wpsc_gateway_is_checked(); ?> name="custom_gateway" class="custom_gateway"/><?php echo wpsc_gateway_name();?>
			<?php endif; ?>
			<?php if(wpsc_gateway_form_fields()): ?>
			<table class='<?php echo wpsc_gateway_form_field_style();?>'>
				<?php echo wpsc_gateway_form_fields();?>
			</table>
			<?php endif; ?>
		</div>
		<div class="clear"></div>
		<?php endwhile; ?>
	<?php else: // otherwise, there is no choice, stick in a hidden form ?>
		<?php while (wpsc_have_gateways()) : wpsc_the_gateway(); ?>
			<input name='custom_gateway' value='<?php echo wpsc_gateway_internal_name();?>' type='hidden' />
			<?php if(wpsc_gateway_form_fields()): ?>
			<table>
				<?php echo wpsc_gateway_form_fields();?>
			</table>
			<?php endif; ?>	
		<?php endwhile; ?>				
	<?php endif; ?>
	<?php if(get_option('terms_and_conditions') != '') : ?>
		<div class="in">
			<input type='checkbox' value='yes' name='agree' /> <?php echo TXT_WPSC_TERMS1;?><a class='thickbox' target='_blank' href='<?php
			echo get_option('siteurl')."?termsandconds=true&amp;width=360&amp;height=400'"; ?>' class='termsandconds'><?php echo TXT_WPSC_TERMS2;?></a>
		</div>
		<div class="clear"></div>
	<?php endif; ?>
	</div>
	<hr class="hrbar"/>
	<?php if(get_option('terms_and_conditions') == '') : ?>
		<input type='hidden' value='yes' name='agree' />
	<?php endif; ?>	
	<?php if(count($wpsc_gateway->wpsc_gateways) == 1 && $wpsc_gateway->wpsc_gateways[0]['name'] == 'Noca') : ?>
	<?php else : ?>
		<input type='hidden' value='submit_checkout' name='wpsc_action' />
		<button class="broap"><?php echo TXT_WPSC_MAKEPURCHASE;?></button>
	<?php endif; ?>
</form>
</div>
<?php else : ?>
<h2><?php echo TXT_WPSC_BUYPRODUCTS; ?>
<?php endif; ?>
<?php else : ?>
<h2>
<?php echo TXT_WPSC_MUST_BE_LOGGED_IN; ?>
</h2>
<form name="loginform" id="loginform" action="<?php echo get_option('siteurl'); ?>/wp-login.php" method="post"><br />
	<label>Username:<br /><input type="text" name="log" id="log" value="" size="20" tabindex="1" /></label><br />
	<label>Password:<br /> <input type="password" name="pwd" id="pwd" value="" size="20" tabindex="2" /></label>
	<p>
		<label>
			<input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="3" /> 
			Remember me
		</label>
	</p>
	<p class="submit">
		<input type="submit" name="submit" id="submit" value="Login &raquo;" tabindex="4" />
		<input type="hidden" name="redirect_to" value="<?php echo get_permalink(); ?>" />
	</p>
</form>
<?php endif; ?>
<?php do_action('wpsc_bottom_of_shopping_cart'); ?>